package com.sky.service.adminImpl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetMealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * @author 陈锦强
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetMealDishMapper setMealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    @Override
    public PageResult<Setmeal> list(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageResult<Setmeal> result = new PageResult<>();
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());
        List<Setmeal> setmealList=setmealMapper.selectPage(setmealPageQueryDTO);
        Page<Setmeal> p = (Page<Setmeal>) setmealList;
        result.setTotal(p.getTotal());
        result.setRecords(p.getResult());
        return result;
    }

    @Override
    @Transactional
    public void add(SetmealDTO setmealDTO) {
        //将两个entity数据分开处理
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        Long mealId = setmeal.getId();
        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        dishes.forEach(dish->{
            dish.setSetmealId(mealId);
        });
        setMealDishMapper.insert(dishes);
    }

    @Override
    public SetmealVO getByIdWithDishes(Long id) {
        Setmeal setmeal=Setmeal.builder().build();
        // 查起售的套餐信息
        SetmealVO setmealVO=setmealMapper.selectById(id);
        // 查起售套餐包含的菜品
        List<SetmealDish> setmealDishes = setMealDishMapper.getMealBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        List<SetmealDish> dishes = setmealDTO.getSetmealDishes();
        if (dishes!=null){
            //删除之前的
            setMealDishMapper.deleteBySetmealId(setmealDTO.getId());
            dishes.forEach(dish->{
                dish.setSetmealId(setmeal.getId());
            });
            setMealDishMapper.insert(dishes);
        }

    }

    @Override
    public void changeStatus(Integer status, Long id) {
        //包含未起售菜品.套餐不能起售
        List<SetmealDish> setmealDishes = setMealDishMapper.getMealBySetmealId(id);
        setmealDishes.forEach(setmealDish->{
            Dish dish = dishMapper.selectById(setmealDish.getDishId());
            if (Objects.equals(dish.getStatus(), StatusConstant.DISABLE)){
             throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
            }
        });
        Setmeal setmeal= Setmeal.builder()
                .status(status)
                .id(id)
                .build();
        setmealMapper.update(setmeal);
    }

    @Override
    @Transactional
    public void deleteBatches(List<Long> ids) {
        if (ids.isEmpty()){
            return;
        }
        //起售套餐不能删除
        ids.forEach(id->{
            SetmealVO byIdWithDishes = getByIdWithDishes(id);
            if (Objects.equals(byIdWithDishes.getStatus(), StatusConstant.ENABLE)){
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        setmealMapper.deleteBatches(ids);
        setMealDishMapper.deleteBatches(ids);
    }

    @Override
    public List<Setmeal> getSetmealByCategoryId(Long categoryId) {
        Setmeal setmeal=Setmeal.builder()
                .categoryId(categoryId)
                .status(StatusConstant.ENABLE)
                .build();
        List<Setmeal> setmeals = setmealMapper.selectByCategoryId(setmeal);
        return setmeals;
    }

    @Override
    public List<DishItemVO> getDishInSetmealById(Long id) {
        return setmealMapper.selectDishById(id);
    }
}
